<?php

namespace app\manager\model;

use app\common\traits\models\UserOperateCheck;

class CarCalendar extends BaseModel
{
    protected $table = 'caravan_calendar';
    protected $is_trashed = '';

    const CARAVAN_LOCK_STATUS = '10, 11';
    const CARAVAN_SHARE_STATUS = '2';
    const CARAVAN_PRIVATE_STATUS = '1';

    protected static $caravan_status = array(
        'private' => self::CARAVAN_PRIVATE_STATUS,
        'share' => self::CARAVAN_SHARE_STATUS,
    );
    
    use UserOperateCheck;

    /**
     * 关联房车信息
     * @return [type] [description]
     */
    public function caravan()
    {
        return $this->hasOne('app\manager\model\Caravans', 'id', 'caravan_id');
    }
    public function caravans()
    {
        return $this->belongsTo('app\manager\model\Caravans', 'caravan_id', 'id')->setAlias(['car_calendar'=>'carcalendar']);
    }

    public function setCaravanCalendar($id)
    {
    	# code...
    }
    public function getCalenderByStatus($caravan_id, $status = '')
    {
        $info = ['caravan_id' => $caravan_id];
        if ($status) {
            $info = array_merge(['status' => $status], $info);
        }
    	return $this->getInfoList($info);
    }
    //

    /**
     * 筛选房车列表
     * @param  array  $search_info [description]
     * @return [type]              [description]
     */
    public function getLists($search_info = [], $user_type = 2)
    {
        if ($search_info) {
            if (!isset($search_info['sdate']) || !isset($search_info['edate'])) {
                return false;
            }
        }

        $list=model('Caravans'); 
        if (!empty($search_info['city'])) {
            $camp=model('manager/Camps')->where('area',$search_info['city'])->cache(20)->column('id');
            if (!empty($camp)) {
            $park = model('manager/Parks')->where('camp_id','in',$camp)->cache(20)->column('id');
            $list=$list->where('parks_id','in',$park);
            }
            // $park = model('manager/Parks')->where('camp_id','in',$camp)->cache(20)->column('id');  halt($park);
            // $list=$list->where('parks_id','in',$park);
        }
        if (!empty($search_info['rv_type'])) {
            $list=$list->where('car_cates_id','in',$search_info['rv_type']);
        }
    
        $range=explode('-',input('get.load_number'));
        $condition['sofe_num'] = array(array('egt',$range[0]),array('elt',$range[1]));
        $list=$list->where($condition)->column('id');
    
        //halt($list);array_diff
        //获取预约人自己房车并剔除掉
        $user_id = model('user/Users')->getUserId();
        $caravan_id[] = model('user/Cards')->where('user_id',$user_id)->value('caravans_id');
        $list = array_diff($list,$caravan_id);
        //halt($list);
        if (empty($list)) {
            return false;
        }
        $res = $this->with(['caravans' => 
                                function($query) use ($list){
                                    $query->where('id', 'in', $list);
                                }
                            ])->field('carcalendar.*,TIMESTAMPDIFF(DAY, s, "'.$search_info['sdate'].'") as c');

        if (isset($search_info['is_exact']) && $search_info['is_exact'] == true) {
            $res->where(['s' => ['elt', $search_info['sdate']], 'e' => ['egt', $search_info['edate']]])
                ->where('TIMESTAMPDIFF(DAY, s, "'.$search_info['sdate'].'") = 0 OR TIMESTAMPDIFF(DAY, s, "'.$search_info['sdate'].'") > 2');
        }else{
            
            $res->where('"'.$search_info['sdate'].' "BETWEEN s AND e')
                ->whereOr('"'.$search_info['edate'].' "BETWEEN s AND e');

        }
        //$data = $res->where('carcalendar.status', self::CARAVAN_SHARE_STATUS)->where('carcalendar.user_type', $user_type)->paginate(2, false);
        //return $data;
        //halt($data);
        //$data = $this->setInfoListQuery()->order($this->getOrder())->paginate($this->getPageSize(), false, ['query' => $querys]);
        return $res->where('carcalendar.status', self::CARAVAN_SHARE_STATUS)->where('carcalendar.user_type', $user_type)->order(['c' => 'desc'])->select();
        
    }


    /**
     * 筛选房车列表
     * @param  array  $search_info [description]
     * @return [type]              [description]
     */
    private function getListsOther($search_info = [])
    {
        if ($search_info) {
            if (!isset($search_info['sdate']) || !isset($search_info['edate']) || !isset($search_info['status'])) {
                return false;
            }
        }
        $user_type = 0;
        if ($user_type = model('User')->getUserType()) {
            # code...
        }
        dump(self::view('caravan_calendar', ['id' => 'id', "TIMESTAMPDIFF(DAY, s, '".$search_info['sdate']."')" => 'c'])
            ->view('caravans_cn', 'name', 'caravans_cn.id = caravan_calendar.caravan_id')
            ->where('s', 'BETWEEN', [$search_info['sdate'], $search_info['edate']])
            ->whereOr('e', 'BETWEEN', [$search_info['sdate'], $search_info['edate']])
            ->order(['c' => 'desc'])
            ->select());
        
    }
    public function oldFun($value='')
    {
        // $ss = model('manager/Caravans', 'model')->cache(true)->column('id');

        // $s = input('sdate');
        // $e = input('edate');
        
        // $rs = $this->with('caravan')->field('caravan_id, TIMESTAMPDIFF(DAY, s, "'.$s.'") as c')->where('caravan_id', 'in', $ss)->where('s', 'elt', $s)->where('e', 'egt', $e)->where('TIMESTAMPDIFF(DAY, s, "'.$s.'") = 0 OR TIMESTAMPDIFF(DAY, s, "'.$s.'") > ts/3')->order(['c' => 'desc'])->cache(true, 10)->select();

        // return $rs;
    }
}
